package com.ys.bean.support;

import com.ys.exception.BeanDefinitionStoreException;
import com.ys.io.Resource;
import com.ys.io.ResourceLoader;

import java.util.Set;

/**
 * <p>@description:抽象的bean定义读取器</p>
 *
 * @projectName: Spring-Mini
 * @packageName: com.com.ys.bean.support
 * @className:
 * @author: shuangyang
 * @date: 1/14/17 11:07 PM
 */
public abstract class AbstractBeanDefinitionReader implements BeanDefinitionReader {
   /* *//*注册的BeanDefinition键值对*//*
    private Map<String, BeanDefinition> registry;*/

    /*bean definitions registry*/
    private final BeanDefinitionRegistry registry;
    /*资源记载器*/
    private ResourceLoader resourceLoader;

    public AbstractBeanDefinitionReader(BeanDefinitionRegistry registry) {
        this.registry = registry;
        resourceLoader = new ResourceLoader();
    }

    /**
     * Return the bean factory to register the bean definitions with.
     *
     * @return
     */
    public BeanDefinitionRegistry getRegistry() {
        return registry;
    }

    /**
     * Set the ResourceLoader to use for resource locations.
     *
     * @param resourceLoader
     */
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    /**
     * Return the Resource loader to use for resource locations.
     *
     * @return
     */
    public ResourceLoader getResourceLoader() {
        return resourceLoader;
    }


    /**
     * Load bean definitions from the specified resources.
     *
     * @param resources
     * @return
     * @throws BeanDefinitionStoreException
     */
    public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {
      /*  Assert.notNull(resources,"Resource array must not be null");*/
        int counter = 0;
        for (Resource resource : resources) {
            counter += loadBeanDefinitions(resource);
        }
        return counter;
    }

    /**
     * load bean definitions from the specified resource location;
     *
     * @param location the resource location,to be loaded with ResourceLoader
     * @return
     * @throws BeanDefinitionStoreException
     */
    public int loadBeanDefinitions(String location) throws BeanDefinitionStoreException {
        return loadBeanDefinitions(location, null);
    }

    /**
     * Load bean definitions from the specified resource location.
     *
     * @param location       the resource location,to be loaded with ResourceLoader
     * @param acutlResources a set to be filled with the actual Resource objects.
     * @return
     */
    public int loadBeanDefinitions(String location, Set<Resource> acutlResources) throws BeanDefinitionStoreException {
        /*Can only load single resources by absolute URL*/
        ResourceLoader resourceLoader = getResourceLoader();
        Resource resource = resourceLoader.getResource(location);
        int loadCount = loadBeanDefinitions(resource);
        return loadCount;

    }

    /**
     * load bean definitions from the specified resource locations;
     *
     * @param locations the resource location,to be loaded with ResourceLoader
     * @return
     * @throws BeanDefinitionStoreException
     */
    public int loadBeanDefinitions(String... locations) throws BeanDefinitionStoreException {
            /*  Assert.notNull(locations,"location array must not be null");*/
        int counter = 0;
        for (String location : locations) {
            counter += loadBeanDefinitions(location);
        }
        return counter;
    }
}
